package com.wf.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.aliyun.facebody20191230.Client;
import com.aliyun.teaopenapi.models.Config;
import com.wf.properties.AliPayProperties;
import com.wf.properties.AliYunProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 支付宝支付配置类
 * @author mpk
 */

@Configuration
@EnableConfigurationProperties(AliPayProperties.class)
@Slf4j
public class AlibabaConfig {

    // Bean注释：创建并配置AlipayConfig对象
    @Bean
    public AlipayClient alipayClient(AliPayProperties aliPayProperties){

        AlipayConfig aliPayConfig = new AlipayConfig();

        aliPayConfig.setServerUrl(aliPayProperties.getServerUrl());
        aliPayConfig.setAppId(aliPayProperties.getAppId());
        aliPayConfig.setPrivateKey(aliPayProperties.getAppPrivateKey());
        aliPayConfig.setAlipayPublicKey(aliPayProperties.getAlipayPublicKey());

        AlipayClient alipayClient;
        try {
            // 初始化支付宝客户端
            alipayClient = new DefaultAlipayClient(aliPayConfig);

        } catch (AlipayApiException e) {
            // 抛出运行时异常，通知调用者支付宝客户端初始化失败
            throw new RuntimeException(e);
        }

        return alipayClient;
    }

    @Bean
    public Client createClient(AliYunProperties aliYunProperties) throws Exception {


                Config config = new Config()
                .setAccessKeyId(aliYunProperties.getAccessKeyId())
                .setAccessKeySecret(aliYunProperties.getAccessKeySecret());
        // 访问的域名
        config.endpoint = "facebody.cn-shanghai.aliyuncs.com";
        return new Client(config);
    }

}

